package com.tianma315.admin.device.controller;


import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Calendar;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.ResponseUtil;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.device.vo.DeviceAndTypeVO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.device.domain.DeviceDO;
import com.tianma315.admin.device.service.DeviceService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 设备表
 * </pre>
 * <small> 2019-05-22 16:08:48 | Aron</small>
 */
@Controller
@RequestMapping("/device/device")
public class DeviceController extends BaseController{
	@Autowired
	private DeviceService deviceService;


	@GetMapping()
	@RequiresPermissions("device:device:device")
	String Device(){
	    return "device/device/device";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("device:device:device")
	public Result<Page<DeviceAndTypeVO>> list(Integer pageNumber, Integer pageSize, DeviceDO deviceDTO){
		Page<DeviceAndTypeVO> page = deviceService.getDeviceAndTypeVOPage(pageNumber,pageSize,deviceDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("device:device:add")
	String add(){
	    return "device/device/add";
	}

	@GetMapping("/edit/{deviceId}")
	@RequiresPermissions("device:device:edit")
	String edit(@PathVariable("deviceId") Integer deviceId,Model model){
		DeviceDO device = deviceService.selectById(deviceId);
		model.addAttribute("device", device);
	    return "device/device/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("device:device:add")
	public Result<String> save( DeviceDO device){
		device.setCreateBy(getUserId());
		device.setCreateDate(Calendar.getInstance().getTime());
		if(deviceService.addDeviceDO(device)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("device:device:edit")
	public Result<String>  update( DeviceDO device){
		deviceService.updateById(device);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("device:device:remove")
	public Result<String>  remove( Integer deviceId){
		if(deviceService.deleteDeviceDO(deviceId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("device:device:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] deviceIds){
		for (int i = 0; i < deviceIds.length; i++) {
			deviceService.deleteDeviceDO(deviceIds[i]);
		}
		return Result.ok();
	}


	/**
	 * 导出模板
	 */
	@GetMapping( "/exportDevice")
	public void exportDevice(HttpServletResponse response){
		String fileName = "设备列表模板.xls";
		try {
			response = ResponseUtil.getResponse(response,fileName);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		HSSFWorkbook wb =deviceService.exportDevice();
		try(OutputStream os = response.getOutputStream()) {
			wb.write(os);
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 进入导入excel
	 */
	@GetMapping( "/deviceImport")
	public String deviceImport(MultipartFile file){
		return "device/device/deviceImport";
	}

	/**
	 * 导入excel
	 */
	@ResponseBody
	@PostMapping( "/importDevice")
	public Result importDevice(MultipartFile file){
		if (!file.isEmpty()){
			deviceService.importDevice(file,getUserId());
		}
		return Result.ok();
	}



	
}
